<template>
	<view>
		<view class="order-main">
			<radio-group class="head-bar" @change="radioChange">
				<label class="label" :class="{'active':item.value==current}" v-for="(item, index) in items" :key="item.value">
					<view>
						<radio :value="item.value" :checked="item.value === current" />
					</view>
					<view>{{item.name}}</view>
				</label>
			</radio-group>
		</view>

		<view class="center" v-for="item in orderslist" :key="item.id">
			<view class="center-on">
				<view class="centeron-left">
					订单编号:{{item.orderId}}
				</view>
				<view class="centeron-right">
					<span>{{orderStatusText}}</span>
				</view>
			</view>
			<view class="center-mid">
				<view class="centermid-left">
					<image :src="item.subList[0].cover_img" mode=""></image>
				</view>
				<view class="centermid-right">
					<view class="midron">
						<span>{{item.subList[0].gname}}</span> <span>¥{{item.subList[0].price/100}}</span>
					</view>
					<view class="midrunder">
						编号 <span>{{item.subList[0].sku_code}}</span> <span>x{{item.subList[0].count}}</span><br>

					</view>
					规格 {{item.subList[0].color_text}}
				</view>
			</view>
			<view class="center-under">
				<view class="centerunder-l">
					时间:{{item.addTime}}
				</view>
				<view class="centerunder-r">
					合计:{{item.subList[0].price/100}}元
				</view>


			</view>
			<view class="btnbox" v-if="current=='0'">
				<button class="btn1" type="default" @click="closeorder(item.orderId)">取消订单</button>
				<button class="btn" type="default" @click="gopay(item.orderId)">付款</button>
			</view>

		</view>


	</view>
</template>

<script>
	import HomeApi from '@/api/mine/index.js'
	export default {
		name: 'ooorder',
		data() {
			return {
				userId: '',
				current: '1',
				orderlist: [],
				backlist: [],
				orderBackList: [],
				items: [{
						name: '待付款',
						value: '0'
					}, {
						name: '已付款',
						value: '1'
					},
					{
						name: '已发货',
						value: '2'
					},
					{
						name: '已收货',
						value: '3'
					},
					{
						name: '申请退款',
						value: '9'
					}
				],
			};
		},
		computed: {
			orderslist() {

				if (this.current == 9) {
					console.log(222)
					return this.orderBackList
				} else {
					return this.orderlist.filter(item => item.status == this.current)

				}

			},
			orderStatusText() {
				var text = '未付款';
				switch (this.current) {
					case '0':
						text = '未付款'
						break;
					case '1':
						text = '已付款'
						break;
					case '2':
						text = '已发货'
						break;
					case '3':
						text = '已收货'
						break;
					case '9':
						text = '申请退款'

						break;

				}
				return text
			}
		},
		methods: {
			getorderlist() {
				HomeApi.orderlist({
					userId: this.userId
				}).then((res) => {
					this.orderlist = res.data.data.list


				})
			},
			getorderBackList() {
				HomeApi.orderBackList({
					userId: this.userId
				}).then((res) => {
					this.backlist = res.data.data
					console.log(this.backlist)
					for (var i = 0; i < this.backlist.length; i++) {
						var item = this.backlist[i];
						this.orderBackList.push({
							orderId: item.orderId,
							process: item.process,
							// 后台需要更新backPrice  后台bug
							totalPrice: item.count * item.back_price,
							addTime:item.addtime,
							subList: [{
								...item
							}]
						})
					}

					console.log(res, '11')
				})
			},
			radioChange: function(evt) {

				for (let i = 0; i < this.items.length; i++) {
					
					if (this.items[i].value === evt.target.value) {
						if (i == 4) {
							this.current = '9';
							break;
						} else {
							this.current = i;
							break;
						}

					}
				}
				// window.location.hash = '#/pages/mine/myorder/myorder?type=' + this.current
			},


			gopay(orderId) {
				uni.navigateTo({
					url: '../order/order?id=' + orderId
				});
				console.log(111)
			},

			closeorder(orderId) {
				HomeApi.delOrderPre({
					orderId
				}).then((res) => {
					if (res.data.code == 'S') {
						uni.showToast({
							title: '取消成功',
							duration: 2000
						});

						uni.navigateBack({
							delta: 0
						})
					} else {
						uni.showToast({
							title: '请重试',
							duration: 2000,
							icon: 'none'
						});
					}
				})
			}
		},
		onLoad(option) {
			this.current = option.type
			this.userId = uni.getStorageSync('storage_key').data.id;
			this.getorderlist()
			this.getorderBackList()
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #f1ece7;
	}

	.order-main {

		.head-bar {
			width: 100%;

			// #ifdef H5
			top: 80rpx;
			// #endif
			display: flex;
			justify-content: space-around;
			width: 100%;
			height: 62rpx;
			padding-top: 14rpx;
			background-color: #354E44;
			color: #fff;
			font-size: 30rpx;
			line-height: 62rpx;
			z-index: 999;
			overflow: hidden;

			.label {
				radio {
					display: none;

				}

				&.active {
					border-bottom: 2rpx solid #DD524D;
					color: #DD524D;
				}

			}
		}
	}

	.center {

		width: 90%;
		height: 500rpx;
		background: #fff;
		border-radius: 40rpx;

		margin: 0 auto;
		margin-top: 40rpx;

		.center-on {
			width: 90%;
			margin: 0 auto;

			display: flex;
			justify-content: space-between;
			font-size: 30rpx;
			border-bottom: 1px solid #F1ECE7;
			padding-bottom: 30rpx;
		}

		.center-mid {
			display: flex;
			justify-content: space-around;
			width: 90%;
			height: 300rpx;
			margin: 0 auto;
			overflow: hidden;
			border-bottom: 1px solid #F1ECE7;
			padding-bottom: 10rpx;

			.centermid-left {
				width: 200rpx;
				height: 200rpx;
				background: blue;
				margin-top: 50rpx;

				image {
					width: 200rpx;
					height: 200rpx;
				}
			}

			.centermid-right {
				margin-top: 50rpx;
				width: 400rpx;
				height: 200rpx;
				color: #8f8f94;
				font-size: 30rpx;
				line-height: 70rpx;

				.midron {
					display: flex;
					justify-content: space-between;
				}

				.midrunder {
					display: flex;
					justify-content: space-between;
				}
			}
		}

		.center-under {
			width: 90%;
			margin: 0 auto;
			display: flex;
			justify-content: space-between;
			font-size: 30rpx;
		}
	}

	.btnbox {
		margin-top: 10rpx;
		display: flex;
		justify-content: right;

		.btn1 {
			font-size: 25rpx;
			text-align: center;
			line-height: 50rpx;
			margin-right: -250rpx;
			width: 160rpx;
			height: 50rpx;
			border: 1px solid;
		}

		.btn {
			font-size: 25rpx;
			text-align: center;
			line-height: 50rpx;
			margin-right: 20rpx;
			width: 150rpx;
			height: 50rpx;
			border: 1px solid;
		}
	}
</style>
